﻿using Stylet;
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WinsionApp.Models.Message;

namespace WinsionApp.Pages
{
    public class PageTestViewModel : Screen,IHandle<MyEvent>
    {
        /// <summary>
        /// 事件集线器
        /// </summary>
        private readonly IEventAggregator _eventAggregator;
        /// <summary>
        /// Ioc容器
        /// </summary>
        private readonly IContainer _container;
        private readonly IWindowManager _windowManager;
       
        /// <summary>
        /// 构造函数
        /// </summary>
        public PageTestViewModel(IEventAggregator eventAggregator, IWindowManager windowManager, IContainer container)
        {
            _eventAggregator = eventAggregator;
            _windowManager = windowManager;
            _container = container;
        }


        public string ReceivedMessage { get; set; }

        public void Handle(MyEvent message)
        {
            ReceivedMessage = message.Message;
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            // 订阅消息
            _eventAggregator.Subscribe(this);
        }


        public void BulletMsg()
        {
            try
            {
                var BulletViewModel = _container.Get<BulletViewModel>();
                BulletViewModel.Title = "快刀E面";
                //result用于接收showDialog返回值
                bool? result = false;
                var root = Window.GetWindow(View).DataContext as Stylet.IViewAware;
                result = _windowManager.ShowDialog(BulletViewModel, root);

                if (result == true)
                {
                    MessageBox.Show("successed");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        public void ShowMessagebox()
        {
            var result = this._windowManager.ShowMessageBox("Hello");
        }
    }
}
